﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SharpShare.Storage;
using SharpShare.Storage.Security;

namespace SharpShare.Afp.Protocol.Handlers {
    internal class AfpCloseForkRequestHandler : IAfpRequestHandler {
        #region IAfpRequestHandler Members

        public byte CommandCode {
            get { return 4; }
        }

        public AfpResultCode Process(IAfpSession session, DsiHeader dsiHeader, AfpStream requestStream, AfpStream responseStream) {
            requestStream.ReadPadding();

            short forkId = requestStream.ReadInt16();

            IAfpFork fork = session.GetFork(forkId);

            if (fork == null) {
                return AfpResultCode.FPObjectNotFound;
            }

            fork.Close();

            //StorageAccessList access = fork.DataProvider.File.GetEffectiveAccess(session);

            //if (access.HasRight(StorageAccessRight.ModifyAttributes)) {
            //    try {
            //        if (fork.BytesWritten > 0) {
            //            fork.DataProvider.File.DateModified = DateTime.Now;
            //        } else {
            //            fork.DataProvider.File.DateModified = dateBeforeClose;
            //        }
            //    } catch { }
            //}

            return AfpResultCode.FPNoErr;
        }

        #endregion
    }
}
